package me.caofeng.controller;

import com.github.pagehelper.PageInfo;
import me.caofeng.enums.Role;
import me.caofeng.pojo.Page;
import me.caofeng.pojo.Product;
import me.caofeng.pojo.Response;
import me.caofeng.service.api.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.controller
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-25 0:13
 * @Version: V1.0
 */
@RequestMapping(value = "product")
@Controller
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequestMapping(value = "search", method = RequestMethod.GET)
    @ResponseBody
    public Response<PageInfo<List<Product>>> getProducts(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "c", required = false) Integer category,
            @RequestParam(value = "key", required = false) String key) {
        return productService.getProductsAsPageInfo(page, size, category, key, null,Role.MEMBER);
    }

    @RequestMapping(value = "search/{page}", method = RequestMethod.GET)
    @ResponseBody
    public Response<Page<List<Product>>> getProductsWithoutPageIndicator(
            @PathVariable("page") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "c", required = false) Integer category,
            @RequestParam(value = "key", required = false) String key) {
        return productService.getProductsAsPage(page, size, category, key, null,Role.MEMBER);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    @ResponseBody
    public Response<Product> getProduct(@PathVariable("id") Integer id){
        return productService.getProduct(id,Role.MEMBER);
    }

}
